package com.training1.api.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.training1.core.dto.APIResultDto;
import com.training1.core.dto.APIShowWithDto;
import com.training1.core.dto.APIWithDrawDto;
import com.training1.core.entity.BatchWithDrawalEntity;
import com.training1.core.entity.MemberEntity;
import com.training1.core.modules.service.BatchWithDrawalService;
import com.training1.core.modules.service.MemberService;
import com.training1.util.SequenceNumberUtil;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import cn.jpush.api.utils.StringUtils;

@Api(value = "/api/withdraw", description = "演示如何使用API文档") // Swagger annotation
@RestController
@RequestMapping("/api/withdraw")
public class APIWithDrawController {
	private static final Logger logger = LoggerFactory.getLogger(APIWithDrawController.class);
	@Resource
	private BatchWithDrawalService bwdService;
	@Resource
	private MemberService memService;
	
	
	@ApiOperation(value = "api会员提现信息查询, 参数是会员手机号码")
	@RequestMapping(value = "/getWithDraw", method = RequestMethod.GET)
	public APIResultDto<APIWithDrawDto> getWithDraw( @RequestParam String cellPhone) {
		APIResultDto<APIWithDrawDto> dto = new APIResultDto<APIWithDrawDto>();
		APIWithDrawDto wdDto = new APIWithDrawDto();
		//根据电话号码查找member对象
		MemberEntity mem = bwdService.findMemberByCell(cellPhone);
		if(mem !=null ){
			if(StringUtils.isEmpty(mem.getBankCardId())){
				dto.setStatus(APIResultDto.ERROR);
				dto.setErrorCode("2");
				dto.setMessage("请先绑定银行卡后在提出提现申请");
				return dto;
			}else{
				wdDto.setAvaibleCash(mem.getAvaibleCash().doubleValue());
				wdDto.setBankForCard(mem.getBankName());
				wdDto.setCardNumber(mem.getBankCardId());
				wdDto.setCellPhone(mem.getCellPhone());
				wdDto.setNameForCard(mem.getMemberName());
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("cellphone", cellPhone);
				//根据电话号码查找申请金额总和
				BatchWithDrawalEntity bde = bwdService.findAllMoney(map);
				if(bde !=null ){
					wdDto.setApplyingCash(bde.getMoney());
				}else{
					wdDto.setApplyingCash(0.00);
				}
				dto.setData(wdDto);
				dto.setStatus(APIResultDto.SUCCESS_OK);
				dto.setErrorCode("0");
				dto.setMessage("获取信息成功");
				return dto;
			}
		}else{
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("1");
			dto.setMessage("账号不存在或已被锁定，请联系平台管理员");
			return dto;
		}
		
	}
	
	@ApiOperation(value = "api会员提现, 第一个参数是会员手机号码，第二个参数是本次提现金额")
	@RequestMapping(value = "/submitWithDraw", method = RequestMethod.GET)
	public APIResultDto<BatchWithDrawalEntity> submitWithDraw(@RequestParam String cellPhone,@RequestParam Double withdrawCash) {
		APIResultDto<BatchWithDrawalEntity> dto = new APIResultDto<BatchWithDrawalEntity>();
		//根据电话号码查找member对象
		MemberEntity mem = bwdService.findMemberByCell(cellPhone);
		//单笔提现需要超过100元
		if(withdrawCash<100){
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("4");
			dto.setMessage("单笔提现需要超过100元");
			return dto;
		}		
		if(mem !=null ){
			//账户余额不足
			if((mem.getAvaibleCash().doubleValue()-withdrawCash)<0){
				dto.setStatus(APIResultDto.ERROR);
				dto.setErrorCode("3");
				dto.setMessage("账户余额不足");
				return dto;
			}
			else if(StringUtils.isEmpty(mem.getBankCardId())){
				dto.setStatus(APIResultDto.ERROR);
				dto.setErrorCode("2");
				dto.setMessage("请先绑定银行卡后在提出提现申请");
				return dto;
			}
			else{
				BatchWithDrawalEntity bde =new BatchWithDrawalEntity();
				//封装条件用于查询当天提现总和
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("cellphone", cellPhone);
				Date date = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				String dateString = formatter.format(date);
				map.put("createTimeFrom", dateString+" 00:00:00");
				map.put("createTimeTo",dateString+" 23:59:59");
				//根据电话号码查找申请金额总和
				BatchWithDrawalEntity bde1 = bwdService.findAllMoney(map);
				if(bde1 !=null ){
					//当天提现金额不得大于50000
					if((bde1.getMoney()+withdrawCash)>50000){
						dto.setStatus(APIResultDto.ERROR);
						dto.setErrorCode("5");
						dto.setMessage("当天提现金额不得超过50000元");
						return dto;
					}
				}
				//将数据封装用于插入数据库
				bde.setOrderNumber(SequenceNumberUtil.generateOrderNumber1());
				bde.setPhone(cellPhone);
				bde.setMoney(new Double(withdrawCash));
				bde.setName(mem.getMemberName());
				bde.setCardNumber(mem.getBankCardId());
				bde.setBankforCard(mem.getBankName());
				bde.setStatus(1);
				bde.setPayStatus(0);
				bde.setCreateBy(cellPhone);
				bde.setUpdateBy(cellPhone);
				
				mem.setAvaibleCash(new BigDecimal(mem.getAvaibleCash().doubleValue()-withdrawCash));
				//插入提现表数据和更新会员表中可提现金额
				boolean flag = bwdService.saveOne(bde, mem)>0;
				if(flag){
					dto.setData(bde);
					dto.setStatus(APIResultDto.SUCCESS_OK);
					dto.setErrorCode("0");
					dto.setMessage("提现申请提交成功");
					return dto;
				}else{
					dto.setStatus(APIResultDto.ERROR);
					return dto;
				}
			}
		}
		else{
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("1");
			dto.setMessage("账号不存在或已被锁定，请联系平台管理员");
			return dto;
		}
	}
	
	@ApiOperation(value = "api会员提现信息列表查询, 第一个参数是会员手机号码，第二个参数是开始记录数")
	@RequestMapping(value = "/getWithDraw1", method = RequestMethod.GET)
	public APIResultDto<List<APIShowWithDto>> getWithDraw1(@RequestParam String cellPhone,Integer begin){
		APIResultDto<List<APIShowWithDto>> dto = new APIResultDto<List<APIShowWithDto>>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("cellPhone", cellPhone);
		if(begin==null){
			begin=0;
		}
		map.put("begin", begin);
		//定义集合用来存放APIShowWithDto
		List<APIShowWithDto> dtoList = new ArrayList<APIShowWithDto>();
		//根据条件查找提现记录
		List<BatchWithDrawalEntity> bwdList = bwdService.findApiList(map);
		//将查询结果封装到APIShowWithDto
		if(bwdList.size()>0){
			for (BatchWithDrawalEntity bde : bwdList) {
				APIShowWithDto showDto = new APIShowWithDto();
				showDto.setApplyDate(bde.getApplyDate());
				showDto.setApplyTime(bde.getApplyTime1());
				showDto.setStatus(bde.getStatusText());
				showDto.setCash(bde.getMoney());
				dtoList.add(showDto);
			}	
			dto.setData(dtoList);
			dto.setStatus(APIResultDto.SUCCESS_OK);
			dto.setErrorCode("0");
			dto.setMessage("提现申请提交成功");
			return dto;
		}
		else{
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("1");
			dto.setMessage("你还没有提现记录");
			return dto;
		}
		

	}
	
	
	
}
